<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index_Controller extends CI_Controller {

	function __construct() {
        parent::__construct();
        $this->load->model('panel/loginModel');
    }

	public function index()
	{

		if($this->session->userdata('UserID')){
			$this->load->view('panel/dashboard');
		}else{
			$this->load->view('panel/index');
		}
	}

	public function dashboard(){
		$this->load->view('panel/dashboard');
	}

	public function sign_in() 
	{
		if($this->session->userdata('UserID')){
			redirect('admin_panel/index/dashboard');
		}else{
			$this->load->view('panel/index');
		}
	}

	public function perform_signin()
	{
		if($this->input->post())
		{
			$user_exists = $this->loginModel->check_credentials($this->input->post('username'), $this->input->post('password'));

			if($user_exists == false)
			{
				$this->session->set_flashdata('error', 'Invalid username and password');
				redirect('admin_panel/index/sign_in');
			}

			$this->session->set_userdata('UserID',  $this->loginModel->id);
			$this->session->set_userdata('RoleName', $this->loginModel->role);
			$this->session->set_userdata('RoleID', $this->loginModel->role_id);
			$this->session->set_userdata('FirstName', $this->loginModel->firstname);
			$this->session->set_userdata('LastName', $this->loginModel->lastname);

			redirect('admin_panel/index/dashboard');
		}
	}

	#logout
	function logout () {
        $this->session->unset_userdata ('UserID');
        $this->session->unset_userdata ('RoleName');
        $this->session->unset_userdata ('RoleID');
        $this->session->unset_userdata ('FirstName');
        $this->session->unset_userdata ('LastName');

        redirect('admin_panel/index/sign_in');
    }

}

/* End of file index_controller.php */
/* Location: ./application/controllers/panel/index_controller.php */